"""配置管理模块"""
import os
from pathlib import Path
from dotenv import load_dotenv

# 加载环境变量
env_path = Path(__file__).parent.parent / '.env'
load_dotenv(env_path)


class Settings:
    """系统配置"""
    
    # LLM配置
    LLM_PROVIDER = os.getenv('LLM_PROVIDER', 'openai')
    OPENAI_API_KEY = os.getenv('OPENAI_API_KEY', '')
    OPENAI_API_BASE = os.getenv('OPENAI_API_BASE', 'https://api.openai.com/v1')
    OPENAI_MODEL = os.getenv('OPENAI_MODEL', 'gpt-4-turbo-preview')
    ANTHROPIC_API_KEY = os.getenv('ANTHROPIC_API_KEY', '')
    ANTHROPIC_MODEL = os.getenv('ANTHROPIC_MODEL', 'claude-3-sonnet-20240229')
    
    # Chainlit配置
    CHAINLIT_HOST = os.getenv('CHAINLIT_HOST', '0.0.0.0')
    CHAINLIT_PORT = int(os.getenv('CHAINLIT_PORT', 8000))
    CHAINLIT_AUTH_SECRET = os.getenv('CHAINLIT_AUTH_SECRET', '')
    
    # 缓存配置
    CACHE_DIR = Path(os.getenv('CACHE_DIR', 'cache'))
    CACHE_TTL = int(os.getenv('CACHE_TTL', 86400))
    
    # 记忆配置
    MEMORY_MAX_HISTORY = int(os.getenv('MEMORY_MAX_HISTORY', 10))
    ENABLE_MEMORY = os.getenv('ENABLE_MEMORY', 'true').lower() == 'true'
    
    # 日志配置
    LOG_LEVEL = os.getenv('LOG_LEVEL', 'INFO')
    LOG_DIR = Path(os.getenv('LOG_DIR', 'logs'))
    
    # AkShare配置
    AKSHARE_TIMEOUT = int(os.getenv('AKSHARE_TIMEOUT', 30))
    
    # 系统配置
    MAX_WORKERS = int(os.getenv('MAX_WORKERS', 5))
    REQUEST_TIMEOUT = int(os.getenv('REQUEST_TIMEOUT', 60))
    
    @classmethod
    def ensure_directories(cls):
        """确保必要的目录存在"""
        cls.CACHE_DIR.mkdir(parents=True, exist_ok=True)
        cls.LOG_DIR.mkdir(parents=True, exist_ok=True)
        (cls.CACHE_DIR / 'bond_data').mkdir(exist_ok=True)


# 创建全局配置实例
settings = Settings()
settings.ensure_directories()